package com.td.framework.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.td.framework.webmanager.domain.AppsysDmb;
import com.td.framework.webmanager.domain.DMbean;

public class BaseDao extends HibernateDaoSupport implements IBaseDao {
    /**
     * 增加数据
     * @param entity 持久化的实体类
     */
	public boolean saveEntity(Object entity){
		 boolean flag = true;
		 try {
			this.getHibernateTemplate().save(entity);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			flag = false;
			e.printStackTrace();
		}
		 return flag;
	}
	
	/**
	 * 删除 持久化的实体对象
	 * @param entity 持久化实体对象
	 */
	public boolean deleteEntity(Object entity){
		boolean flag = true;		 
		try {
			this.getHibernateTemplate().delete(entity);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = false;
		}
		return flag;    		
	}
	
	/**
	 * 删除持久化对象根据一个主键
	 * @param clazz
	 * @param id
	 */
	
	public void deleteById(Class clazz,long id){
	       try {
			Object obj = this.getHibernateTemplate().get(clazz, id);
			this.getHibernateTemplate().delete(obj);
		} catch (DataAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 删除持久化对象根据多个主键
	 * @param clazz
	 * @param ids
	 * @return
	 */	
	public boolean deleteBatch(Class clazz ,long[] ids){
		boolean flag = true;
		try {
			for(long id :ids){
				this.deleteById(clazz, id);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			flag =false;
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
	 *增加或者修改实体
	 * @param t
	 * @return	
	 */	
   public boolean saveOrUpdate(Object entity){
		 boolean flag = false;
		 try {
			this.getHibernateTemplate().saveOrUpdate(entity);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 return flag;
    }
	
	/**
	 * 根据hql语句得到集合
	 * @param queryString
	 * @return
	 */
	public List getListByHql(String queryString){
	    return this.getHibernateTemplate().find(queryString);	
	}
	
	/**
	 * 根据序列化id得到实体类
	 * @param clazz 实体类
	 * @param  id 序列化主键
	 */
	public Object getEntityById(Class clazz, long id){
		return this.getHibernateTemplate().get(clazz, id);
	}
	
	/**
	 *sql数量语句得到总记录数;
	 *@param sql 求数量的sql语句 
	 * @return rows;
	 */
	@SuppressWarnings("unchecked")
	public int getRowsBySql(String sql) {
		int rows = 0;	
		Session session = getSession();
		try {
			rows = Integer.parseInt(session.createSQLQuery(sql).uniqueResult().toString());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}
    
	/**
	 *  根据hql语句得到数量结果
	 * @param hql 查询数量的hql语句
	 * @return
	 */
	public int getRowsByHql(String hql){
		int rows= 0;
		Session session = getSession();
		try {
			rows =  Integer.parseInt(session.createQuery(hql).uniqueResult().toString());
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}
	
	/**
	 *  根据hql语句得到数量结果
	 * @param hql 查询list的hql语句
	 * @return
	 */
	public int getRowsByListHql(String hql){
		int rows= 0;
		Session session = getSession();
	    try {
			rows =  this.getHibernateTemplate().getFetchSize();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return rows;
	}
	
	/**
	 * 根据hql语句得到分页后的集合	
	 * @param queryString
	 * @param startRow
	 * @param pageSize
	 * @return
	 */
		
	  public List getPageList(String queryString ,int startRow,int pageSize){
		  List list = new ArrayList();
		try {
			Session session = getSession();
			  Query query = session.createQuery(queryString);
			  query.setFirstResult(startRow);
			  query.setMaxResults(pageSize);
			  list = query.list();
		} catch (DataAccessResourceFailureException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return list;
	  }   
	  
		/**
		 * 根据sql语句得到键值对集合,要只有两列结果第一列为key第二列为value
		 * @param sql SQL语句
		 */
		public List  getDmbeansBySql(String sql){
			Session session = this.getSession();
			List list = session.createSQLQuery(sql).list();
			List<DMbean> dmBeanList = new ArrayList<DMbean>(); 
			for(int i = 0 ;i<list.size();i++){
		       Object[] obj = (Object[]) list.get(i);		
		       DMbean dmbean = new DMbean();
		       dmbean.setKey(obj[0]==null?"":obj[0].toString());
		       dmbean.setValue(obj[1]==null?"":obj[1].toString());
		       dmBeanList.add(dmbean);
			}
			return dmBeanList;
		}
		
		/**
		 * @toDo:通过代码类型查询代码信息;
		 * @param dmlx
		 * @return list
		 */		
		public List<AppsysDmb> getDmbByDmlx(String dmlx) {		
		   String sql = "from com.td.framework.webmanager.domain.AppsysDmb t where t.dmlx='"+dmlx+"'";		
			List<AppsysDmb> list = this.getHibernateTemplate().find(sql);
			return list;
		}

		public boolean update(Object entity) {
			// TODO Auto-generated method stub
			boolean flag =false;
			this.getHibernateTemplate().update(entity);
			flag = true;
			return flag;
		}

		
		
	}


